import { defineStore } from 'pinia';
import { UserState } from '../interface';
import { isEmpty, isNotEmpty, isUnDef } from '@/utils/is';
import { getCache, removeCache, setCache } from '@/utils/cache';
import { TOKEN_KEY, USER_INFO_KEY } from '@/enums/cacheEnum';

const enum SuperAdminEnum {
  YES = '0',
  NO = '1',
}

export const useUserStore = defineStore('uniapp-user', {
  state: (): UserState => ({
    token: '',
    userInfo: {
      id: '',
      username: '',
      gender: '',
      status: '',
    },
  }),
  getters: {
    // 获取token
    getToken: (state) => () => {
      const token = getCache<string>(TOKEN_KEY);
      if (isUnDef(token)) {
        return state.token || '';
      }
      return token;
    },
    // 获取用户信息
    getUserInfo: (state) => () => {
      const user = getCache<User.UserInfo>(USER_INFO_KEY);
      if (isUnDef(user)) {
        return state.userInfo;
      }
      return user;
    },
    // 是否登录
    isLogin: (state): boolean => {
      if (isEmpty(state.token)) {
        const token = getCache<string>(TOKEN_KEY);
        return !!token;
      }
      return !!state.token;
    },
    // 是否超级管理员
    isSuperAdmin: (state) => () => {
      return (
        isNotEmpty(state.userInfo.superAdmin) && state.userInfo.superAdmin == SuperAdminEnum.YES
      );
    },
    // 显示名称
    displayName: (state) => () => {
      return state.userInfo.realName && isNotEmpty(state.userInfo.realName)
        ? state.userInfo.realName
        : state.userInfo.nickName
          ? state.userInfo.nickName
          : '';
    },
  },
  actions: {
    initToken() {
      const token = getCache<string>(TOKEN_KEY) || undefined;
      if (token) {
        this.token = token;
      }
    },
    setToken(token: string | undefined) {
      if (token) {
        setCache(TOKEN_KEY, token);
        this.token = token;
      } else {
        removeCache(TOKEN_KEY);
        this.token = '';
      }
    },
    setUserInfo(user: User.UserInfo | undefined) {
      if (user) {
        setCache(USER_INFO_KEY, user);
        this.userInfo = user;
      } else {
        removeCache(USER_INFO_KEY);
        this.userInfo = { id: '', username: '', gender: '', status: '' };
      }
    },
    removeToken() {
      removeCache(TOKEN_KEY);
      removeCache(USER_INFO_KEY);
      this.setToken(undefined);
      this.setUserInfo(undefined);
    },
    /**
     * @description 登录
     */
    // async login(params: Login.ReqLoginForm): Promise<HttpResponse<ResultData<LoginModel>>> {
    //   try {
    //     this.removeToken();
    //     const res = await login(params);
    //     const { data } = res.data;
    //     this.setToken(data.token);
    //     this.setUserInfo(data.sysUser);
    //     return Promise.resolve(res);
    //   } catch (err: any) {
    //     return Promise.reject(err);
    //   }
    // },
    /**
     * @description 登出
     */
    // async loginOut(): Promise<any> {
    //   try {
    //     const res = await logout();
    //     this.removeToken();
    //     return Promise.resolve(res);
    //   } catch (err: any) {
    //     return Promise.reject(err);
    //   }
    // },
    /**
     * @description 刷新token
     */
    // async refreshToken(): Promise<Login.ResLogin> {
    //   try {
    //     const { data } = await refreshToken();
    //     this.setToken(data.access_token);
    //     return Promise.resolve(data.data);
    //   } catch (err: any) {
    //     return Promise.reject(err);
    //   }
    // },
  },
});
